package com.wisdytech.linkdcs.equipment.action;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.common.handler.ResourceMatchingService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.common.utils.DataTransformUtil;
import com.wisdytech.linkdcs.equipment.model.AlarmRecord;
import com.wisdytech.linkdcs.equipment.model.EquipAlarmParam;
import com.wisdytech.linkdcs.equipment.service.IEquipmentAlarmService;
import com.wisdytech.linkdcs.equipment.vo.EquipmentAlarmDefinitionVO;
import com.wisdytech.linkdcs.equipment.vo.EquipmentAlarmRecordVO;
import com.wisdytech.linkdcs.system.model.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 
 * <p>ClassName: EquipAlarmAction</p>
 * <p>Description: 设备预警管理</p>
 * <p>Author: dailibo</p>
 * <p>Date: 2017-10-10</p>
 */
@Controller
@RequestMapping("/equipment/alarm")
public class EquipAlarmAction {
    
    @Resource
    private IEquipmentAlarmService equipmentAlarmService;

    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;
    /**
     * 
     * <p>Description: 预警定义</p>
     * @param model
     * @return
     */
    @RequestMapping("/definition/index")
    public String definitionIndex(Model model, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        return "equipment/equipAlarm/index";
    }
    
    /**
     * 
     * <p>Description: 分页获取预警定义列表 </p>
     * @return
     */
    @RequestMapping("/definition/list/get.mvc")
    @ResponseBody
    public StandardTableResult<EquipmentAlarmDefinitionVO> listDefinitions(Filter filter,String eqCode,String equipName){
        
        PageInfo<EquipAlarmParam>  pageInfo =
                equipmentAlarmService.listDefinitions(filter, eqCode, equipName);
        
        List<EquipAlarmParam> alarmParams =
                    pageInfo.getList();
        
        List<EquipmentAlarmDefinitionVO> alarmParamVOs =new ArrayList<>();
        
        for(EquipAlarmParam param : alarmParams){
            alarmParamVOs.add(DataTransformUtil.changeEquipmentAlarmDefinitionVO(param));
        }
        
        return new StandardTableResult<EquipmentAlarmDefinitionVO>(pageInfo.getTotal(),alarmParamVOs);
    }
    
    @RequestMapping("/addOrUpdateDefinition/add.mvc")
    @ResponseBody
    public StandardResult addDefinition(EquipAlarmParam param,HttpSession session){
	
    	SysUser  user=BaseUtil.getCurrentUser();

        if(StringUtils.isBlank(param.getPkId())){
        	
        	
            param.setCreateUser(user.getUsername());
            param.setCreateDate(new Date());
            return equipmentAlarmService.add(param);
        }else{
            param.setUpdateUser(user.getUsername());
            param.setUpdateDate(new Date());
            return equipmentAlarmService.update(param);
        }
    }
    
    @RequestMapping("/delDefinition/del.mvc")
    @ResponseBody
    public StandardResult delDefinition(HttpSession session,String pkIds){
        
        
        SysUser user=BaseUtil.getCurrentUser();
        
        List<String> ids =
                JSON.parseArray(pkIds, String.class);
        //批量删除
        List<EquipAlarmParam> alarmParams =new ArrayList<>();
        
        for(String id :ids){
            EquipAlarmParam param =new EquipAlarmParam();
            param.setPkId(id);
            param.setDelFlag(CommonConstants.DEL_FLAG_TRUE);
            param.setUpdateDate(new Date());
            param.setUpdateUser(user.getUsername());
            alarmParams.add(param);
        }
        
        return equipmentAlarmService.delete(alarmParams);
        
    }
    
    
    
    /**
     * 
     * <p>Description: 预警查询</p>
     * @param model
     * @return
     */
    @RequestMapping("/search/index")
    public String definition1Index(Model model, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        return "equipment/equipAlarm/query";
    }
    
    /**
     * 
     * <p>Description: 分页获取预警记录</p>
     * @param filter
     * @param startTime
     * @param endTime
     * @param eqCode
     * @return
     */
    @RequestMapping("/listAlarmRecord/get.mvc")
    @ResponseBody
    public StandardTableResult<EquipmentAlarmRecordVO> listDefinitionRecord(
            Filter filter,String startTime,String endTime,String eqCode){
        PageInfo<AlarmRecord> pageInfo=
                equipmentAlarmService.listAlarmRecord(filter, eqCode, startTime, endTime);
        List<AlarmRecord> lists=pageInfo.getList();
        
        List<EquipmentAlarmRecordVO> vos =new ArrayList<>();
        for(AlarmRecord record : lists){
            vos.add(DataTransformUtil.changeEquipmentAlarmRecordVO(record));
        }
        
        return new StandardTableResult<>(pageInfo.getTotal(), vos);
    }

    /**
     * 处理预警记录
     * @param record
     * @return
     */
    @RequestMapping("/dealAlarmRecord/update.mvc")
    @ResponseBody
    public StandardResult dealAlarmRecord(AlarmRecord record){
        SysUser operator = BaseUtil.getCurrentUser();
        equipmentAlarmService.dealAlarmRecord(record,operator);
        return StandardResult.getSuccessResult();

    }
    
    @RequestMapping("/updateAlarmRecord/update.mvc")
    @ResponseBody
    public StandardResult updateAlarmRecord(AlarmRecord record){
        
        return equipmentAlarmService.update(record);
        
    }
    
    
    
}
